package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo.Enum.QuestionType;
import com.example.demo.entity.*;
import com.example.demo.service.QuestionService;
import com.example.demo.service.QuestiontypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("Question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    //这里的逻辑是传入数据后我再进行导入 导入成功后我再进行 返回 返回的东西 应该是200 或500 前端展示需要导入的数据 然后确定导入 导入后在后端进行处理 然后显示导入成功的数据

    @PostMapping("/readExcel")
    public ResponseEntity readExcel(@RequestParam("file") MultipartFile file,String courseId) throws IOException {
        System.out.println(courseId);
        questionService.ChoiceE(file.getInputStream(),courseId);
        return ResponseEntity.ok("");
    }

    @PostMapping("/readExcel1")
    public ResponseEntity readExcel1(@RequestParam("file") MultipartFile file){
        return ResponseEntity.ok("a");
    }

    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("ExcelModel/question.xlsx");
        InputStream inputStream = classPathResource.getInputStream();
        Workbook workbook = new XSSFWorkbook(inputStream);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode("question.xlsx", "utf-8"));
        response.setHeader("Access-Control-Expose-Headers", "content-Disposition");
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }
}